﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization.Users;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.IdentityFramework;
using Abp.Localization;
using Abp.MultiTenancy;
using Microsoft.AspNet.Identity;
using XHD.WXZJ.Departments;

namespace XHD.WXZJ.Posts
{
    /// <summary>
    /// 岗位管理
    /// </summary>
    /// <typeparam name="TTenant"></typeparam>
    /// <typeparam name="TDepartment"></typeparam>
    /// <typeparam name="TUser"></typeparam>
    /// <typeparam name="TPost"></typeparam>
    public class PostsManager<TTenant, TDepartment, TUser, TPost> : IDomainService

        where TTenant : AbpTenant<TTenant, TUser>
        where TDepartment : AbpDepartment<TTenant, TUser>
        where TUser : AbpUser<TTenant, TUser>
        where TPost : Posts<TTenant, TDepartment, TUser>
    {

        public ILocalizationManager LocalizationManager { get; set; }
        protected IRepository<TPost> PostRepository { get; set; }

        /// <summary>
       /// 构造函数
       /// </summary>
        /// <param name="postRepository"></param>
       public PostsManager(IRepository<TPost> postRepository)
        {
            PostRepository = postRepository;
        }

       private string L(string name)
       {
           return LocalizationManager.GetString(WXZJConsts.LocalizationSourceName, name);
       }
        /// <summary>
        /// 岗位对象校验
        /// </summary>
       /// <param name="post"></param>
        /// <returns></returns>
       protected virtual async Task<IdentityResult> ValidatePostAsync(TPost post)
       {
           var nameValidationResult = await ValidatePostNameAsync(post.Name);
           return !nameValidationResult.Succeeded ? nameValidationResult : IdentityResult.Success;
       }

       /// <summary>
       /// 名称校验
       /// </summary>
       /// <param name="postName"></param>
       /// <returns></returns>
       protected virtual async Task<IdentityResult> ValidatePostNameAsync(string postName)
       {
           return !Regex.IsMatch(postName, Posts<TTenant, TDepartment, TUser>.PostNameRegex) ? AbpIdentityResult.Failed(L("InvalidPostNameName")) : IdentityResult.Success;
       }

        /// <summary>
       /// 获取所有对象
       /// </summary>
       public virtual IQueryable<TPost> Posts
       {
           get { return PostRepository.GetAll(); }
       }

        /// <summary>
        /// 新建对象
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public virtual async Task<IdentityResult> CreateAsync(TPost post)
        {
            if (await PostRepository.FirstOrDefaultAsync(d => d.Name == post.Name) != null)
            {
                return AbpIdentityResult.Failed(string.Format(L("PostNameIsAlreadyTaken")));
            }

            var validationResult = await ValidatePostAsync(post);
            if (!validationResult.Succeeded)
            {
                return validationResult;
            }
            await PostRepository.InsertAsync(post);

            return IdentityResult.Success;
        }


        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public virtual async Task<IdentityResult> UpdateAsync(TPost post)
        {
            if (await PostRepository.FirstOrDefaultAsync(t => t.Name == post.Name && t.Id != post.Id) != null)
            {
                return AbpIdentityResult.Failed(string.Format(L("PostNameIsAlreadyTaken"), post.Name));
            }

            await PostRepository.UpdateAsync(post);
            return IdentityResult.Success;

        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public virtual async Task<IdentityResult> DeleteAsync(TPost post)
        {
            //1.先删除岗位用户表里面对应的数据
            //await PostUserRepository.DeleteByPostIdAsync(post.Id);
            //2.再删除岗位
            await PostRepository.DeleteAsync(post);
            return IdentityResult.Success;
        }

        public virtual async Task<TPost> GetByIdAsync(int postId)
        {
            var department = await PostRepository.FirstOrDefaultAsync(postId);
            if (department == null)
            {
                throw new AbpException(string.Format("不存在部门id为{0}的岗位", postId));
            }
            return department;
        }

        public virtual async Task<TPost> FindByIdAsync(int postId)
        {
            return await PostRepository.FirstOrDefaultAsync(postId);
        }
        /// <summary>
        /// 根据岗位名称查找岗位对象
        /// </summary>
        /// <param name="postName">岗位名称</param>
        /// <returns></returns>
        public virtual async Task<TPost> FindByNameAsync(string postName)
        {
            return await PostRepository.FirstOrDefaultAsync(
                post => post.Name == postName
                );
        }

        /// <summary>
        /// 根据显示名称查找岗位对象
        /// </summary>
        /// <param name="displayName">显示名称</param>
        /// <returns></returns>
        public virtual async Task<TPost> FindByDisplayNameAsync(string displayName)
        {
            return await PostRepository.FirstOrDefaultAsync(
                post => post.DisplayName == displayName
                );
        }
        //public virtual void Dispose()
        //{
        //    //如果使用IOC则要在这里做释放
        //}
    }
}
